<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<!-- SKU样式 -->
<link href='${BASE_PATH}/static/css/sku_style.css' media='all'
	rel='stylesheet' type='text/css'/>
<head>
    <title>管理平台</title>
    <jsp:include page="../../../include/header.jsp"></jsp:include>
        <style type="text/css">
			
		 	table td {
	text-align: center;
	white-space: normal;
	overflow: hidden;
	text-overflow: ellipsis;
}

table th {
	text-align: center;
}
	        .single-line-text {			   
			    margin-left: 70px;
			    position: relative;
			}
			.allchoose{
			       display: inline-block;
				    position: relative;
				    top: 26px;
				    left: 15px;
			}
			.allchoose input{
			      vertical-align: top;
			}
			.share-title{
			    position: absolute;
			    right: 13rem;
			    height: 27px;
			    color: #646464;
			    cursor: pointer;
			    border: 1px solid #9e9b9b;			    
			    padding: 2px 5px;
			    border-radius: 5px;
			   
			}
			.goods-title {
				position: fixed;
			    width: 157px;
			    /* position: absolute; */
			    background: #fff;
			    list-style: none;
			  /*   right: 9rem;
			    top: 4rem; */
			    right: 13rem;
   				top: 21rem;
			    border: 1px solid #ccc4c4;
			    padding-left: 0;
			    text-align: center;
			    padding-bottom: 10px;
			    display: none;
			    z-index: -999;
			}
			.goods-title li{
		        height: 25px;
			    line-height: 25px;				   
			    text-align: center;
			    margin-top: 8px;			    
			}
             .goods-title li span{
			    background: #c5cbce;
			    color: #fff;
			    padding: 3px 9px;
			    border-radius: 3px;
			    cursor: pointer;
             }
             .goods-title-tip{
                background: #31c3da;
			    color: #fff;
			    padding: 3px 5px;
			    border-radius: 3px;
			    cursor: pointer;
             }
             .GoodsTitleOn,.title-tip-on{
                background: #00b8ff !important;
             }
             .btn-title{
                background: linear-gradient(to bottom,#5cb85c 0,#428bca 100%);
			    border: 1px solid;
			    color: #fff;
			    border-radius: 5px;
			    padding: 5px 5px 5px 10px;
			    letter-spacing: 5px;
			    margin-top: 8px;
             }
             .btn-reset{
                 background:#c5cbce;
			    border: 1px solid;
			    color: #fff;
			    border-radius: 5px;
			    padding: 5px 5px 5px 10px;
			    letter-spacing: 5px;
			    margin-top: 8px;
             }
		</style>
</head>
<body>
<div id="wrapper" class="row">

			<div class="col-lg-12">
				<h1 class="page-header">商品上架</h1>
			</div>
		<!-- 搜索框 -->
			<div class="title-top col-lg-12">
				<label class="search-label">商品编号：</label><input type="text" id="sGoodId" value="" class="form-control search-input width200" maxlength="21">
				<label class="search-label">商品名称：</label><input type="text" id="sGoodName" value="" class="form-control search-input width200" >
				<label class="search-label">选择公司：</label>
				<select id="sSellerId" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="120">
						<c:forEach items="${list }" var="seller">
							<option value="${seller.sellerId }">${seller.sellerName }</option>
						</c:forEach>
					</select>
				<label class="search-label">选择分公司：</label>
				<select id="sStoreId" class="selectpicker pull-left p-l-5" multiple="multiple" data-width="120" >
				</select>
				<label class="search-label">商品状态：</label>
				<div class="search-input">
					<select id="sState" class="selectpicker" data-width="150">
						<option value="0">全部</option>
						<option value="1">上架审核中</option>
						<option value="2">已上架</option>
						<option value="3">已下架</option>
						<option value="4">驳回</option>
					</select>
				</div>
				<div class="clearfix"></div>
				<label class="search-label" >商品属性：</label>
				<div class="search-input">
					<select id="sGb" class="selectpicker" data-width="150">
						<option value="0">全部</option>
						<option value="1">常规</option>
						<option value="2">团购</option>
					</select>
				</div>
				<button class="btn btn-success search-btn" onclick="query();">确定</button>
				<button class="btn btn-success search-btn" onclick="reset();">重置</button>
				<div class=" pull-right">
					<shiro:hasPermission name="bus:goods:base:doExcel">
						<button onclick="doExcel();"  class="btn btn-success search-btn pull-right">导出</button>
					</shiro:hasPermission>
					<button class="btn btn-success search-btn pull-right" onclick="toAdd()">上架</button>
					<button class="btn btn-default search-btn pull-right" title="刷新" onclick="window.location.reload();"> 刷新</button>
					<button type="button" class="btn btn-success pull-right margin-lr" onclick="toEditAll()"><span class="fa "></span>通过本页</button>
				</div>
			<div class="col-lg-12 title-top">
				<div class="row" style="position:relative;margin-left:10px;">
						<div class="text-muted  pull-left">共 <font color="#428bca" id="dataCount">0</font> 条记录</div>
				</div>
				<div class="table-responsive panel panel-default">
					<table id="dataTable" class="table table-hover table-bordered" style="overflow:scroll;">
						<thead>
							<tr>
								<th width="80px;">客户页面</th>
								<th width="300px;">操作</th>
								<th width="100px;">缩略图</th>
								<th width="80px" ><input type="checkbox" id="selectAll" />全选</th>
								<th width="80px;">商品ID</th>
								<th width="300px;">商品名称</th>
								<th width="150px;">商品类别</th>
								<th width="80px;">商品属性</th>
								<th width="80px;">是否拓客</th>
								<th width="200px;">发布公司</th>
								<th width="160px;">申请时间</th>
								<th width="120px;">上架分公司数</th>
								<th width="160px;">审核时间</th>
								<th width="160px;">下架时间</th>
								<th width="80px;">当前状态</th>
							</tr>
						</thead>
						<tbody id="dataList"></tbody>
					</table>
				</div>
	        	<div class="col-lg-12">
        			<div class = "pull-right">
	        			<div class="text-muted  pull-left" style="margin-top:20px; margin-right:18px;">
							<select id="pageSize" name="pageSize" class="selectpicker" data-width="100" onchange="query()" >
		    					<option value="10" selected = "selected">10条/页</option>
								<option value="15">15条/页</option>
								<option value="20">20条/页</option>
								<option value="50">50条/页</option>
								<option value="100">100条/页</option>
		    				</select>
						</div>
		        		<ul id="dataPagination" class="pagination-sm pull-right"></ul>
		        	</div>
	        	</div>
			</div>
	</div>
</div>
<jsp:include page="../../../include/javascripts.jsp"></jsp:include>
<!-- 异步加载下一页数据后，用模板渲染 -->
<script type="text/html" id="thumbTpl">
<a href="javascript:void(0)" onclick="Kit.photo('{{thumb}}')" class="center-block thumbnail thumbnail-none-margin width50"><img src="{{thumb}}"></a>
</script>
<script type="text/html" id="dataTpl">
	{{each list as item}}
	<tr id="item{{item.id}}" data-id="{{item.id}}">
		{{if item.groupbuying==1}}
			<td><a href=" http://yphapi.nczywc.cn/h5/#/pages/product/product?id={{item.id}}" target="_blank"><span class="btn">链接</span></a></td>
		{{else if item.groupbuying==2}}
			<td><a href=" http://yphapi.nczywc.cn/h5/#/pages/product/productGroup?ID={{item.id}}&userId=undefined " target="_blank"><span class="btn">链接</span></a></td>
		{{/if}}
		{{if item.pstate==1}}
			{{if item.state==1}}
				<td>
					<a href="javascript:showGoods('{{item.id}}');"><span class="btn">详情</span></a>
					<a href="javascript:doAccept('{{item.id}}');"><span class="btn">通过</span></a>
					<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					<a href="javascript:toCancel('{{item.id}}');"><span class="btn">驳回</span></a>
				</td>
			{{else if item.state==2}}
				<td>
					<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					<a href="javascript:doDown('{{item.id}}');"><span class="btn">下架</span></a>
					<a href="javascript:showGoods('{{item.id}}');"><span class="btn">详情</span></a>
				</td>
			{{else if item.state==3}}
				<td>
					<a href="javascript:showGoods('{{item.id}}');"><span class="btn">详情</span></a>
					<a href="javascript:doUp('{{item.id}}');"><span class="btn">上架</span></a>
					<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					<a href="javascript:sale('{{item.id}}');"><span class="btn">销售信息</span></a>
					<a href="javascript:Tem('{{item.id}}');"><span class="btn">模板</span></a>

					<a href="javascript:doDelete('{{item.id}}');"><span class="btn">删除</span></a>
					<%--  --%>
				</td>
			{{else if item.state==4}}
				<td>
					<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
					<a href="javascript:showGoods('{{item.id}}');"><span class="btn">详情</span></a>
					<a href="javascript:doDelete('{{item.id}}');"><span class="btn">删除</span></a>
				</td>
			{{/if}}
		{{else if item.pstate==2}}
			<td>
				<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
				<a href="javascript:showGoods('{{item.id}}');"><span class="btn">详情</span></a>
			</td>
		{{else if item.pstate==3}}
			<td>
				<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
				<a href="javascript:showGoods('{{item.id}}');"><span class="btn">详情</span></a>
			</td>
		{{/if}}
		<td><a href="javascript:void(0)" onclick="Kit.photo('${FTP_PATH}{{item.thumb}}')" class="center-block thumbnail thumbnail-none-margin width50"><img src="${FTP_PATH}{{item.thumb}}"></a></td>
		<td><input type="checkbox" name="checkcode" value="{{item.id}}" ></td>
		<td>{{item.id}}</td>
		<td>{{item.name}}</td>
		<td>{{item.oneName}}</td>
		<td>{{item.groupbuying | flagTransform:1,'常规',2,'团购'}}</td>
		<td style="text-align: center">
			{{if item.tState}}<span style="color: #428bca;">打开</span>{{/if}}
			{{if !item.tState}}关闭{{/if}}
		</td>
		<td>{{item.sellerName}}</td>
		<td>{{item.createDate}}</td>
		<td>{{item.countStore}}</td>
		<td>{{item.checktime}}</td>       
		{{if item.pstate==1 && item.state==3}}
			<td>{{item.downtime}}</td>
		{{else}}
			<td></td>
		{{/if}}
		{{if item.pstate==1}}
			<td>
				{{if item.state==1}}
					上架审核中
				{{else if item.state==2}}
					<span style="color: #428bca;">已上架</span>
				{{else if item.state==3}}
					<span style="color: #a94442;">已下架</span>
				{{else if item.state==4}}
					<span style="color: #a94442;">被驳回</span>
				{{/if}}
			</td>
		{{else if item.pstate==2}}
			<td>售罄</td>
		{{else if item.pstate==3}}
			<td>降权</td>
		{{/if}}
		</tr>
	{{/each}}
</script>
<script type="text/javascript">
//<td> <input type="checkbox" value="{{item.id}}" class="checkbox-title"> </td>

var goodsId="";//准备分配标签的商品Id
var dataPaginator;
$(query);
function query(){
	var pageSize=$("#pageSize").val();
	var sGoodId = $("#sGoodId").val(),sGoodName = $("#sGoodName").val(),sSellerId = $("#sSellerId").val(), sStoreId = $("#sStoreId").val(), sState = $("#sState").val(),sGb = $("#sGb").val();
	var sell,store;
	if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
		sell=sSellerId.join("','");
	}
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	if(dataPaginator){
		dataPaginator.destroy();
	}
	dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/goods/base/page",{sGb:sGb,sGoodId:sGoodId,sGoodName:sGoodName,sSellerId:sell,sStoreId:store,sState:sState,pageSize:pageSize},function(result){
		//设置显示最新的数据数量
		$("#dataCount").html(result.object.totalRow);
		//根据模板渲染数据并填充
		$("#dataList").empty().append(template("dataTpl",result.object));
	});
}

function choiceReset(){
	var tip=document.querySelectorAll(".goods-title-tip"),
	  goodsTitle=document.querySelector(".goods-title");
		 goodsTitle.style.display="none";
		 goodsTitle.style.zIndex="-999";
		 goodsTitle.style.opacity="0";
		 resetClass(tip,"goods-title-tip");//初始化样式
}


function reset(){
	$("#sGoodId").val("");
	$("#sGoodName").val("");
	$("#sSellerId").val("");$("#sSellerId").trigger("change");  $("#sStoreId").val("");$("#sStoreId").trigger("change");  $("#sState").val("0");$("#sState").trigger("change");
	$("#sStoreId").selectpicker("refresh"); $("#sSellerId").selectpicker("refresh");
}
function doExcel(){
	var sGoodId = $("#sGoodId").val(),sGoodName = $("#sGoodName").val(),sSellerId = $("#sSellerId").val(), sStoreId = $("#sStoreId").val(), sState = $("#sState").val();
	var sell="",store="";
	if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
		sell=sSellerId.join("','");
	}
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	window.location.href="${BASE_PATH}/bus/goods/base/doExcel?sGoodId="+sGoodId+"&sGoodName="+sGoodName+"&sSellerId="+sell+"&sStoreId="+store+"&sState="+sState;
}
var detailDialog;
function showGoods(id){
	detailDialog = Kit.dialog("商品详情","${BASE_PATH}/bus/goods/base/goodDetail?id="+id,{closable:true,size:'size-wide'}).open();
}
var addDialog;
function toAdd(){
	addDialog = Kit.dialog("添加","${BASE_PATH}/bus/goods/base/toAdd",{closable:true,size:'size-wide'}).open();
  }

var tokerDialog;
function toToker(id){
	tokerDialog = Kit.dialog("拓客","${BASE_PATH}/bus/goods/base/toToker/"+id,{closable:true,size:'size-wide'}).open();
}
//销售信息
var saleDialog;
function sale(id){
	saleDialog = Kit.dialog("销售信息","${BASE_PATH}/bus/goods/base/sale/"+id,{closable:true,size:'size-wide'}).open();
} 
//模板
var TemDialog;
function Tem(id){
	TemDialog = Kit.dialog("模板信息","${BASE_PATH}/bus/goods/base/Tem/"+id,{closable:true,size:'size-wide'}).open();
} 
var editDialog;
function toEdit(id){
	editDialog = Kit.dialog("修改","${BASE_PATH}/bus/goods/base/toEdit/"+id,{closable:true,size:'size-wide'}).open();
}

function onEditSuccess(newObject){
	$("#item"+newObject.id).replaceWith(template("dataTpl",{"list":[newObject]}));
}
var toCancelDialog;
function toCancel(id){
	toCancelDialog = Kit.dialog("驳回原因","${BASE_PATH}/bus/goods/base/toCancel?id="+id,{closable:true,size:"size-wide:100px"}).open();
}

function doAccept(id){
	Kit.confirm("提示","确定要审核申请吗？",function(){
		$.post("${BASE_PATH}/bus/goods/base/doAccept",{'id':id},function(result){
			if(result.flag==0){
				Kit.alert("操作成功");
				onEditSuccess(result.object);
				/*window.location.reload(true) ;
				getGoodsTitle();
				allotTitle(); */
			}else{
				Kit.alert("操作失败");return;
			}
			
		});
	});
}
function doDown(id){
	Kit.confirm("提示","确定要下架商品吗？",function(ref){
		$.post("${BASE_PATH}/bus/goods/base/doDown",{'id':id},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else{
				Kit.alert("操作失败");return;
			}
			
		});
	});
}
function doClerk(id){
	Kit.confirm("提示","确定要将商品置成售罄吗？",function(ref){
		$.post("${BASE_PATH}/bus/goods/base/doClerk",{'id':id},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else{
				Kit.alert("操作失败");return;
			}
			
		});
	});
}
function doUp(id){
	Kit.confirm("提示","确定要重新上该架商品吗？",function(ref){
		$.post("${BASE_PATH}/bus/goods/base/doUp",{'id':id},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else{
				//Kit.alert("操作失败");return;
				Kit.alert(result.message);return;
			}
		});
	});
}
function doDelete(id){
	Kit.confirm("提示","确定要删除商品吗？",function(ref){
		$.post("${BASE_PATH}/bus/goods/base/delete",{'id':id},function(result){
			if(result.flag==0){
				$("#item"+id).remove();
				Kit.alert("删除成功");return;
			}else{
				Kit.alert(result.message);return;
			}
			
		});
	});
}
 
 $("#sSellerId").change(function(){
	 var sSellerId=[];
	  sSellerId=$("#sSellerId").val();
	  $.ajax({
                 type: "POST",
                 url: "${BASE_PATH}/bus/goods/base/chooseStore",
                 data: {"sSellerId" : sSellerId},
                 dataType: "json",
                 traditional: true,
                 success: function(data){
                	 $("#sStoreId").empty();
                           for(var i=0; i<data.object.length;i++){
                          	 $("#sStoreId").append("<option value='"+data.object[i].storeid+"'>"+data.object[i].title+"</option>");
                           }
                           $("#sStoreId").selectpicker("refresh");
                       }
             });
        }); 
     /*
     *显示与隐藏商品标签
     */
     getGoodsTitle();
     function getGoodsTitle(){
    	 var getGoodsTitle=document.querySelectorAll(".getGoodsTitle");
	     for(var i=0;i<getGoodsTitle.length;i++){
	    	 getGoodsTitle[i].onclick=function(){
	    		 if(this.className=="getGoodsTitle GoodsTitleOn"){
	    			 this.className="getGoodsTitle";
	    		 }else{
	    			 this.className="getGoodsTitle GoodsTitleOn";
	    		 }	    		 
	    	 }
	     }
     }
	 
	    //点击分配标签
	/*      var btnTitle=checkTitle=document.querySelector(".btn-title");
	     btnTitle.onclick=function(){
	    	 allotTitle();
	    	}	 */     
	        //分配标签后重新加载页面
			function allotTitle(){
				var checkTitle=document.querySelectorAll(".checkbox-title");
		    	 var GoodsTitleOn=document.querySelectorAll(".GoodsTitleOn");
		    	 var titleCord=[];
		    	 for(var i=0;i<GoodsTitleOn.length;i++){
		    		 titleCord[i]=GoodsTitleOn[i].getAttribute("tip");
		    	 }
		    	 titleCord=titleCord.join(",");
		    	 console.log("titleCord=="+titleCord);
		    			$.post("${BASE_PATH}/bus/goods/base/addTitle",{"goodsId":goodsId,"titleCord":titleCord},function(result){		    				
		    					Kit.alert("添加成功");
		    					//分配成功后初始化样式
		    					resetClass(GoodsTitleOn,"getGoodsTitle");
		    					var activ=document.querySelector("#dataPagination  .active a").text;
		    					var params = {
		    							pageNum:activ,
		    							pageSize:10
		    						};
		    					dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/goods/base/page",params,function(result){
		    						//设置显示最新的数据数量
		    						$("#dataCount").html(result.object.totalRow);
		    						//根据模板渲染数据并填充
		    						$("#dataList").empty().append(template("dataTpl",result.object));
		    						showGoodsTitle();
		    						$(query);
		    					});
		    				
		    					//分配失败后初始化样式
		    					resetClass(GoodsTitleOn,"getGoodsTitle");
		    					return;	
		    			});
			}

	    	/* 实现全选和全不选 */
			$("#selectAll").on('click', function() {
				$("input[name='checkcode']").prop("checked", this.checked);

			});
			//批量推荐
			function toEditAll() {
				var adIds = "";  
				$("input:checkbox[name=checkcode]:checked").each(function(i){  
					if(0==i){  
						adIds = $(this).val();  
					}else{
						adIds += (","+$(this).val());  
					}
				}); 
				if(adIds==""){
					Kit.alert("请选择要通过审核的商品");
				}else{
					$.post("${BASE_PATH}/bus/goods/base/toEditAll?adIds="+adIds,function(result){
						if(result.flag==0){
							Kit.alert("通过审核成功");
							return;
						}else{
							Kit.alert("操作失败");return;
						}
					});
				}
			}
 
  //初始化（className）样式
	   function resetClass(obj,objName){
		   for(var k=0;k<obj.length;k++){
			   obj[k].className=objName;
	    	 }
	   }
</script>
</body>
</html>